/*****************************************************************************
 *
 * HUSONG PROPRIETARY INFORMATION
 *
 * The information contained herein is proprietary to HUSONG
 * and shall not be reproduced or disclosed in whole or in part
 * or used for any design or manufacture
 * without direct written authorization from HUSONG.
 *
 * Copyright (c) 2015 by HUSONG. All rights reserved.
 *
 *****************************************************************************/
package com.broker.consumer.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import me.send.broker.cons.MessageCons;
import me.send.broker.entity.Broker;
import me.send.broker.entity.Feedback;
import me.send.broker.entity.Message;
import me.send.broker.entity.TrainPic;
import me.send.broker.entity.base.Page;
import me.send.broker.entity.base.ReturnBean;
import me.send.broker.entity.base.ReturnJsonBean;
import me.send.broker.entity.base.TrainTest;
import me.send.broker.enums.AuthodType;
import me.send.broker.pojo.BrokerIndexBean;
import me.send.broker.pojo.BrokerReturnBean;
import me.send.broker.pojo.Data;
import me.send.broker.pojo.OrderMap;
import me.send.framework.util.LoggerUtils;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.broker.consumer.controller.base.BaseController;

/**
 * @author maxmin
 *
 * @date 2015年8月3日 下午5:50:29
 */
@Controller
@RequestMapping(value = "broker")
public class BrokerController extends BaseController {

	@RequestMapping("/login")
	@ResponseBody
	public ReturnBean test(Broker user, String devicenumber) {
		ReturnBean returnBean = new ReturnBean();
		try {
			returnBean = brokerService.login(user, devicenumber);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return returnBean;
	}

	// 根据手机号码 修改密码
	@RequestMapping(value = "update/password")
	@ResponseBody
	public ReturnBean update_password(String telephone, String password,String code,
			String devicenumber) {
		ReturnBean rb = new ReturnBean();
		try {
			int result = brokerService.isRegisted(telephone);
			if (result>0) {
				int a = brokerService
						.update_password(telephone, password,code, devicenumber);
				if (a >= 0) {
					rb.setCode(ReturnBean.SUCCESS);
					rb.setMessage(ReturnBean.SUCCESS_STR);
				} else if(a==-1){
					rb.setCode(ReturnBean.FAIL);
					rb.setMessage("参数错误，不能为空");
				}else if (a==-2) {
					rb.setCode(ReturnBean.FAIL);
					rb.setMessage("请输入验证码");
				}else if (a==-3) {
					rb.setCode(ReturnBean.FAIL);
					rb.setMessage("验证码不正确");
				}
			}else {
				rb.setCode(ReturnBean.FAIL);
				rb.setMessage("帐号不存在");
			}
		} catch (Exception e) {
			saveException(AuthodType.tangjianglong.getName(), "BrokerController", "update_password()", e);
			e.printStackTrace();
		}
		
		return rb;
	}

	// 修改昵称 修改头像 修改性别 2015年7月20日09:44:18
	@RequestMapping(value = "update/username")
	@ResponseBody
	public ReturnBean update_username(String username, int popid,
			String devicenumber) {
		int a = brokerService.update_username(popid, username, devicenumber);
		ReturnBean rb = new ReturnBean();
		if (a >= 0) {
			rb.setCode(ReturnBean.SUCCESS);
			rb.setMessage(ReturnBean.SUCCESS_STR);
		} else {
			rb.setCode(ReturnBean.FAIL);
			rb.setMessage("参数错误");
		}
		return rb;
	}

	@RequestMapping(value = "update/sex")
	@ResponseBody
	public ReturnBean update_sex(int sex, int popid, String devicenumber) {
		int a = brokerService.update_sex(popid, sex, devicenumber);
		ReturnBean rb = new ReturnBean();
		if (a >= 0) {
			rb.setCode(ReturnBean.SUCCESS);
			rb.setMessage(ReturnBean.SUCCESS_STR);
		} else {
			rb.setCode(ReturnBean.FAIL);
			rb.setMessage("参数错误");
		}
		return rb;
	}

	@RequestMapping(value = "/update/info")
	@ResponseBody
	public ReturnBean updateInfo(int sex, String username, int brokerid,
			String devicenumber) {
		ReturnBean rb = new ReturnBean();
		try {
			LoggerUtils.error(getClass().getName(), username);
			int rt = brokerService.updateInfo(sex, username,
					brokerid, devicenumber);
/*			BrokerIndexBean broker = brokerService.updateInfo(sex, username,
					brokerid, devicenumber);
*/			
			if (rt==1) {
				BrokerIndexBean broker = brokerService.getBrokerInfo(brokerid);
				rb.setData(broker);
				rb.setCode(ReturnBean.SUCCESS);
				rb.setMessage(ReturnBean.SUCCESS_STR);
			}else if (rt==2) {
				rb.setMessage("用户名已存在");
			}else if (rt==0) {
				rb.setMessage("修改失败");
			}
		} catch (Exception e) {
			saveException(AuthodType.tangjianglong.getName(),
					"BrokerController", "updateInfo", e);
			e.printStackTrace();
		}
		return rb;
	}

	@RequestMapping(value = "update/headerpath")
	@ResponseBody
	public ReturnBean update_headerpath(String headerpath, int popid,
			String devicenumber) {
		int a = brokerService
				.update_headerpath(popid, headerpath, devicenumber);
		ReturnBean rb = new ReturnBean();
		if (a >= 0) {
			rb.setCode(ReturnBean.SUCCESS);
			rb.setMessage(ReturnBean.SUCCESS_STR);
		} else {
			rb.setCode(ReturnBean.FAIL);
			rb.setMessage("参数错误");
		}
		return rb;
	}

	@RequestMapping("/home")
	@ResponseBody
	public ReturnBean home(Page page, int type) {
		LoggerUtils.info(getClass().getName(), page.getCityname());
		BrokerReturnBean returnBean = new BrokerReturnBean();
		if (page.getCityname()!=null && !"".equals(page.getCityname())) {
			if (openCityService.getCityIsOpenByName( page.getCityname())==1) {
				List<OrderMap> lists = orderService.home(page, type);
				System.out.println(page.getPopid());
				Data num = orderService.countPage(page, type);
				returnBean.setDatas(lists);
				returnBean.setCode(0);
				returnBean.setMessage("Success");
				returnBean.setPagenum(num.getPagenum());
				returnBean.setTotal(num.getNumtotal());
			}else {
				returnBean.setMessage("您所在城市未开放");
			}
		}else {
			returnBean.setMessage("请选择城市");
		}
		return returnBean;
	}

	// 用户注册 发送验证码 用户验证这个手机号是否注册过 用户登录 2015年7月15日10:10:56
	@RequestMapping(value = "register")
	@ResponseBody
	public ReturnBean register(String telephone, String devicenumber,
			String code, String password, HttpServletResponse response,String bindmac,String cityname) {
		return brokerService.register(telephone, devicenumber, code, password, bindmac, cityname);
	}

	@RequestMapping(value = "sendCode")
	@ResponseBody
	public ReturnBean sendCode(String telephone, String devicenumber) {
		return brokerService.sendCode(telephone, devicenumber);
	}

	@RequestMapping(value = "verification")
	@ResponseBody
	public ReturnBean verification(String telephone, String devicenumber) {
		return brokerService.verification(telephone, devicenumber);
	}

	/**
	 * 获取培训内容
	 * 
	 * @param brokerid
	 * @param devicenumber
	 * @return
	 */
	@RequestMapping(value = "/totrain")
	@ResponseBody
	public ReturnBean toTrain(int brokerid, String devicenumber) {
		ReturnBean rb = new ReturnBean();
		try {
			List<TrainPic> trainPics = brokerService.trainPics();
			rb.setDatas(trainPics);
			rb.setCode(ReturnBean.SUCCESS);
			rb.setMessage(ReturnBean.SUCCESS_STR);
		} catch (Exception e) {
			saveException(AuthodType.tangjianglong.getName(),
					"BrokerController", "toTrain()", e);
			e.printStackTrace();
		}
		return rb;
	}

	/**
	 * 经纪人完成培训
	 * 
	 * @param brokerid
	 * @param devicenumber
	 * @return
	 */
	@RequestMapping(value = "/trained")
	@ResponseBody
	public ReturnBean trained(int brokerid, String devicenumber) {
		ReturnBean rb = new ReturnBean();
		int trained = brokerService.trained(brokerid);
		if (trained > 0) {
			rb.setCode(ReturnBean.SUCCESS);
			rb.setMessage(ReturnBean.SUCCESS_STR);
		}
		return rb;
	}

	/**
	 * 获取测试内容
	 * 
	 * @param brokerid
	 * @param devicenumber
	 * @return
	 */
	@RequestMapping(value = "/totest")
	@ResponseBody
	public ReturnBean toTest(int brokerid, String devicenumber) {
		ReturnBean rb = new ReturnBean();
		try {
			List<TrainTest> trainTests = brokerService.trainTests();
			rb.setCode(ReturnBean.SUCCESS);
			rb.setMessage(ReturnBean.SUCCESS_STR);
			rb.setDatas(trainTests);
		} catch (Exception e) {
			saveException(AuthodType.tangjianglong.getName(),
					"BrokerController", "toTest", e);
			e.printStackTrace();
		}
		return rb;
	}

	/**
	 * 经纪人完成测试
	 * 
	 * @param brokerid
	 * @param devicenumber
	 * @return
	 */
	@RequestMapping("/tested")
	@ResponseBody
	public ReturnBean tested(int brokerid, String devicenumber) {
		ReturnBean rb = new ReturnBean();
		int trained = brokerService.tested(brokerid);
		if (trained > 0) {
			rb.setCode(ReturnBean.SUCCESS);
			rb.setMessage(ReturnBean.SUCCESS_STR);
		}
		return rb;
	}

	/**
	 * 经纪人认证
	 * 
	 * @param brokerid
	 * @param devicenumber
	 * @return
	 */
	@RequestMapping("/authen")
	@ResponseBody
	public ReturnBean authen(Broker broker, String devicenumber) {
		ReturnBean rb = new ReturnBean();
		if (broker.getRealName() != null && !"".equals(broker.getRealName())
				&& broker.getAuthenTel() != null
				&& !"".equals(broker.getAuthenTel())
				&& broker.getIdNum() != null && !"".equals(broker.getIdNum())
				&& broker.getPositiveIdPath() != null
				&& !"".equals(broker.getPositiveIdPath())
				&& broker.getNegativeIdPath() != null
				&& !"".equals(broker.getNegativeIdPath())
				&& broker.getHandIdPath() != null
				&& !"".equals(broker.getHandIdPath())) {
			int result = brokerService.authen(broker);
			if (result > 0) {
				rb.setCode(ReturnBean.SUCCESS);
				rb.setMessage(ReturnBean.SUCCESS_STR);
			}
		}
		return rb;
	}

	/**
	 * 获取经纪人信息
	 * 
	 * @param brokerid
	 * @param devicenumber
	 * @return
	 */
	@RequestMapping(value = "/info")
	@ResponseBody
	public ReturnBean info(int brokerid, String devicenumber) {
		ReturnBean rb = new ReturnBean();
		try {
			BrokerIndexBean broker = brokerService.getBrokerInfo(brokerid);
			rb.setCode(ReturnBean.SUCCESS);
			rb.setMessage(ReturnBean.SUCCESS_STR);
			rb.setData(broker);
		} catch (Exception e) {
			saveException(AuthodType.tangjianglong.getName(),
					"BrokerController", "info()", e);
			e.printStackTrace();
		}
		return rb;
	}

	/**
	 * 认证信息详情
	 * 
	 * @param brokerid
	 * @return
	 */
	@RequestMapping(value = "/autheninfo")
	@ResponseBody
	public ReturnBean authenInfo(int brokerid, String devicenumber) {
		ReturnBean rb = new ReturnBean();
		try {
			Broker broker = brokerService.authenInfo(brokerid);
			rb.setCode(ReturnBean.SUCCESS);
			rb.setMessage(ReturnBean.SUCCESS_STR);
			rb.setData(broker);
		} catch (Exception e) {
			saveException(AuthodType.tangjianglong.getName(),
					"BrokerController", "authenInfo()", e);
			e.printStackTrace();
		}
		return rb;
	}

	/**
	 * 经纪人建议反馈
	 * 
	 * @param brokerid
	 * @param devicenumber
	 * @return
	 */
	@RequestMapping(value = "/feedback")
	@ResponseBody
	public ReturnJsonBean feedback(Feedback feedback, String devicenumber) {
		ReturnJsonBean rb = new ReturnJsonBean();
		try {
			brokerService.feedback(feedback);
			rb.setCode(ReturnBean.SUCCESS);
			rb.setMessage(ReturnBean.SUCCESS_STR);
		} catch (Exception e) {
			saveException(AuthodType.tangjianglong.getName(),
					"BrokerController", "authenInfo()", e);
			e.printStackTrace();
		}
		return rb;
	}

	/**
	 * 获取经纪人消息列表
	 * 
	 * @param devicenumber
	 * @param brokerid
	 * @param type
	 *            1:通知消息2：动态消息 
	 * @return
	 * @author maxmin
	 * @date 2015年8月27日下午12:01:27
	 */
	@RequestMapping("/messages")
	@ResponseBody
	public ReturnJsonBean messges(String devicenumber, int brokerid, int type,
			Long startid, int size) {
		ReturnJsonBean rb = new ReturnJsonBean();
		try {
			List<Message> list = new ArrayList<Message>();
			if (type == MessageCons.MESSAGE_TYPE_ORDER) {
				list = messageService.getMessagesByBrokerId(brokerid, startid,size);
			} else if (type > MessageCons.MESSAGE_TYPE_ORDER) {
				list = messageService.getDynamicMessagesByBrokerId(brokerid,startid, size);
			}
			rb.setCode(ReturnJsonBean.SUCCESS);
			rb.setMessage(ReturnJsonBean.SUCCESS_STR);
			rb.setDatas(list);
		} catch (Exception e) {
			saveException(AuthodType.tangjianglong.getName(),
					"BrokerController", "messges()", e);
			e.printStackTrace();
		}
		return rb;
	}
}
